package com.obtk.service.impl;

//import cn.hutool.core.lang.UUID;

import com.obtk.mapper.CstOrderMapper;
import com.obtk.model.CstOrder;
import com.obtk.model.OrderLine;
import com.obtk.modelSGW.CstServer;
import com.obtk.modelSGW.ProProduct;
import com.obtk.modelSGW.ProStorage;
import com.obtk.service.*;
import com.obtk.util.ResultUtil;
import com.obtk.util.UUIDUtil;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-03-31 08:45:37
 */
@Service
public class CstOrderServiceImpl implements CstOrderService {
    @Autowired
    private CstOrderMapper cstOrderMapper;
    @Autowired
    private OrderLineService orderLineService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ProStorageService proStorageService;
    @Autowired
    private ProProduceService proProduceService;
    @Autowired
    private CustomerServerService customerServerService;

    @Override
    public List<CstOrder> selectCstOrder(CstOrder cstOrder) {
        return cstOrderMapper.selectCstOrder(cstOrder);
    }

    @Override
    public List<CstOrder> selectCstOrder1(CstOrder cstOrder) {
        return cstOrderMapper.selectCstOrder1(cstOrder);
    }


    @Override
    public CstOrder selectByCstOrderName(String cstOrderName) {

        return cstOrderMapper.selectByCstOrderName(cstOrderName);
    }

    @Override
    public CstOrder selectByCstOrderId(String odrId) {
        System.out.println("service" + odrId);
        return cstOrderMapper.selectByCstOrderId(odrId);
    }

    @Override
    public int updateByCstOrderId(CstOrder cstOrder) {
        return cstOrderMapper.updateByCstOrderId(cstOrder);
    }

    @Override
    public int updateStatusBatch(List<String> odrIds, Integer odrStatus) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("odrIds", odrIds);
        params.put("odrStatus", odrStatus);
        return cstOrderMapper.updateStatusBatch(params);
    }

    @Override
    public int selectCstOrder(long custId) {
        return cstOrderMapper.selectCstOrder(custId);
    }

    @Override
    @Transactional
    public ResponseVo insert(CstOrder cstOrder, OrderLine orderLine, ProStorage proStorage) {
        System.out.println("orderLine：" + orderLine);
        System.out.println("cstOrder：" + cstOrder);
        String i = UUIDUtil.getUniqueIdByUUId();//生成一个订单号,防止订单号相同
        while (true) {
            CstOrder cstOrder1 = selectByCstOrderName(i);
            if (null != cstOrder1) {
                i = UUIDUtil.getUniqueIdByUUId();
            } else {
                cstOrder.setOdrOrder(i);
                cstOrder.setOdrStatus(0);
                cstOrder.setOdrDate(new Date());
                System.out.println("生成订单编号:" + i);
                //redis超时
                break;
            }
        }
        System.out.println("插入订单表:" + cstOrder);
        int num = cstOrderMapper.insert(cstOrder);//2 新增订单,判断是否成功
        if (num > 0) {
            orderLine.setOddOrderId(cstOrder.getOdrOrder());
            orderLine.setOddStatus(1);
            orderLine.setOddUnit("默认");
            System.out.println("插入订单链接表:" + orderLine);
            int register = orderLineService.register(orderLine);//新增订单链接表
            if (register > 0) {
                proStorage.setStkCount(orderLine.getOddCount());
                proStorage.setStkProdCode(orderLine.getOddProdId());
                System.out.println("减库存:" + proStorage);
                int i2 = proStorageService.updateProStorage(proStorage);// 减库存
                if (i2 > 0) {
                    return ResultUtil.success("创建订单成功!");
                } else {
                    return ResultUtil.error("库存异常");
                }
            } else {
                return ResultUtil.error("订单链接表异常");
            }
        }else{
            return ResultUtil.success("创建订单失败");
        }
    }
    public ResponseVo updateStatusByOdrCode(CstOrder cstOrder, CstServer cstServer){
        int i = cstOrderMapper.updateStatusByOdrCode(cstOrder);
        if(i>0){
            ProProduct proDuct = proProduceService.getProDuct(cstOrder.getOdrOrder());
            System.out.println("产品："+proDuct);
            cstServer.setSvrSerId(proDuct.getProdCode());
            cstServer.setSvrSerType(proDuct.getProdType());
            cstServer.setSvrTitle("默认");
            System.out.println("服务："+cstServer);
            int i1 = customerServerService.addCstServer(cstServer);
            if (i1>0){
                return ResultUtil.success("订单已付款");
            }else{
                return ResultUtil.success("创建客户服务失败");
            }
        }else{
            return ResultUtil.error("支付失败");
        }
    }

// 更改客户的有订单 和 无订单 状态
//            if (register > 0) {
//                int i1 = customerService.updateOrder(cstOrder.getOdrCustId());
//
//                return ResultUtil.success("创建订单成功");
//            } else {
//                return ResultUtil.error("链接表失败");
//            }
//        }else {
//            return ResultUtil.error("创建订单失败");
//        }
//    }
}
